import 'dart:ui';
import 'package:animation_effect_demo/src/effects/base/base_effect.dart';
import 'package:flutter/material.dart';

import 'base/base_operation.dart';


class TextStyleData {
  ///文本内容
  late String content;

  ///字体
  late String fontStyle;

  late String fontName;

  ///字号
  late int fontSize;


  TextStyleData(
      {required this.content, required this.fontStyle, required this.fontSize,required this.fontName});

  TextStyleData.fromJson(Map<String, dynamic> map) {
    content = map['content']??'';
    fontStyle = map['fontStyle']??'';
    fontName = map['fontName']??'unknown';
    fontSize = map['fontSize']??12;
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'content': content,
      'fontStyle': fontStyle,
      'fontName': fontName,
      'fontSize': fontSize,
    };
  }

  TextStyleData copyWith({String? content, String? fontStyle, int? fontSize, String? fontName}) {
    return TextStyleData(
      content: content ?? this.content,
      fontStyle: fontStyle ?? this.fontStyle,
      fontName: fontName ?? this.fontName,
      fontSize: fontSize ?? this.fontSize,
    );
  }
}

enum StyleType { color, effect, none }



class TextFrameStyle {
  late int frameValue;
  late List<Color> colors;

  TextFrameStyle({required this.frameValue, required this.colors});

  TextFrameStyle.fromJson(Map<String, dynamic> map) {
    frameValue = map['frameValue']??0;
    List list = map['colors']??[];
    colors = list.map((e) => Color(e)).toList();
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'frameValue': frameValue,
      'colors': colors.map((e) => e.value).toList(),
    };
  }

  TextFrameStyle copyWith({
    int? frameValue,
    List<Color>? colors,
  }) {
    return TextFrameStyle(
        frameValue: frameValue ?? this.frameValue,
        colors: colors ?? this.colors);
  }
}

class TextAttrData {
  ///字体颜色
  late List<Color> fontColors;

  ///背景样式
  BackgroundStyle? backgroundStyle;

  ///边框index
  TextFrameStyle? textFrameStyle;

  TextAttrData(
      {required this.fontColors, this.textFrameStyle,this.backgroundStyle});

  TextAttrData.fromJson(Map<String, dynamic> map) {
    if (map['fontColors'] != null) {
      List list = map['fontColors'];
      fontColors = list.map((e) => Color(e)).toList();
    }
    if (map['backgroundStyle'] != null) {
      backgroundStyle = BackgroundStyle.fromJson(map['backgroundStyle']);
    }
    if (map['textFrameStyle'] != null) {
      textFrameStyle = TextFrameStyle.fromJson(map['textFrameStyle']);
    }
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'fontColors': fontColors.map((e) => e.value).toList(),
      'backgroundStyle': backgroundStyle?.toJson(),
      'textFrameStyle': textFrameStyle?.toJson(),
    };
  }

  TextAttrData copyWithFontColors(List<Color> fontColors) {
    return TextAttrData(
      fontColors: fontColors,
      textFrameStyle:  textFrameStyle,
    );
  }


  TextAttrData copyWithTextFrameStyle(TextFrameStyle? textFrameStyle) {
    return TextAttrData(
      fontColors: fontColors,
      textFrameStyle: textFrameStyle,
    );
  }
}

enum AnimationType { fixed, up, down, left, right, flicker }

extension AnimationTypeExt on AnimationType {
  String get text {
    switch (this) {
      case AnimationType.up:
        return 'Move up';
      case AnimationType.down:
        return 'Move down';
      case AnimationType.left:
        return 'Move left';
      case AnimationType.right:
        return 'Move right';
      case AnimationType.flicker:
        return 'Twinkle';
      default:
        return 'Fixed';
    }
  }

  int get value {
    switch (this) {
      case AnimationType.up:
        return 2;
      case AnimationType.down:
        return 3;
      case AnimationType.left:
        return 4;
      case AnimationType.right:
        return 5;
      case AnimationType.flicker:
        return 6;
      default:
        return 1;
    }
  }
}

class TextEffectData {
  ///动画类型
  late AnimationType animationType;

  ///动画速度
  late int speed;

  ///亮度
  late int brightness;

  TextEffectData(
      {required this.animationType,
      required this.speed,
      required this.brightness});

  TextEffectData.fromJson(Map<String, dynamic> map) {
    animationType = AnimationType.values[map['animationType']];
    speed = map['speed']??8;
    brightness = map['brightness']??100;
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'animationType': animationType.index,
      'speed': speed,
      'brightness': brightness,
    };
  }

  TextEffectData copyWith(
      {AnimationType? animationType, int? speed, int? brightness}) {
    return TextEffectData(
      animationType: animationType ?? this.animationType,
      speed: speed ?? this.speed,
      brightness: brightness ?? this.brightness,
    );
  }
}

class TextOperation extends BaseOperation {
  ///文本样式
  late TextStyleData textStyleData;

  ///文本属性
  late TextAttrData textAttrData;

  ///文本动效
  late TextEffectData textEffectData;

  TextOperation(
      {required this.textStyleData,
      required this.textAttrData,
      required this.textEffectData});

  TextOperation.fromJson(Map<String, dynamic> map) {
    textStyleData = TextStyleData.fromJson(map['textStyleData']);
    textAttrData = TextAttrData.fromJson(map['textAttrData']);
    textEffectData = TextEffectData.fromJson(map['textEffectData']);
  }

  @override
  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'textStyleData': textStyleData.toJson(),
      'textAttrData': textAttrData.toJson(),
      'textEffectData': textEffectData.toJson()
    };
  }

  TextOperation copyWith(
      {TextStyleData? textStyleData,
      TextAttrData? textAttrData,
      TextEffectData? textEffectData}) {
    return TextOperation(
      textStyleData: textStyleData ?? this.textStyleData,
      textAttrData: textAttrData ?? this.textAttrData,
      textEffectData: textEffectData ?? this.textEffectData,
    );
  }
}
class AnimationEffectTemplate {
  String uuid = '';

  ///模板名称
  String name = '';


  ///是否自定义模板
  late bool isCustom;

  late bool isFavorite;

  ///模板效果类型
  late EffectType effectType;

  ///效果类型id
  int? effectId;

  ///模板设置参数
  Map<String, dynamic>? operation;




  AnimationEffectTemplate(
      this.uuid,
      this.name, {
        required this.effectType,

        this.isFavorite = false,
        this.effectId,
        this.isCustom = false,

        this.operation,
        String? md5,

      });

  AnimationEffectTemplate copyWith({
    String? uuid,
    String? name,

    bool? isFavorite,
    int? effectId,
    EffectType? effectType,
    bool? isCustom,
    Map<String, dynamic>? operation,

  }) {
    return AnimationEffectTemplate(
      uuid ?? this.uuid,
      name ?? this.name,

      isFavorite: isFavorite ?? this.isFavorite,
      effectId: effectId ?? this.effectId,
      effectType: effectType ?? this.effectType,
      isCustom: isCustom ?? this.isCustom,
      operation: operation ?? this.operation,
    );
  }

  AnimationEffectTemplate.getBackgroundEffect(Color color) {
    uuid = '';
    name = '';
    isCustom = true;
    isFavorite = false;
    operation = <String, dynamic>{'color': color.value};
  }

  AnimationEffectTemplate.fromJson(Map<String, dynamic> json) {
    uuid = json['uuid']??'';
    name = json['name']??'';
    isCustom = json['isCustom']??false;
    isFavorite = json['isFavorite']??false;
    effectId = json['effectId'];


    if (json['effectType'] is String) {
      effectType = EffectType.values.firstWhere(
            (element) => element.name == json['effectType'],
      );
    } else {
      effectType = EffectType.values[json['effectType']];
    }




    operation = json['operation'];

  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['uuid'] = uuid;
    data['name'] = name;
    data['isCustom'] = isCustom;
    data['isFavorite'] = isFavorite;
    data['effectId'] = effectId;

    data['effectType'] = effectType.name;
    data['operation'] = operation;
    return data;
  }

  ///  如果默认动画有更新 animationEffectVersion 需要 +1，否则app不会更新动效列表
  static int animationEffectVersion = 1;

}

class BackgroundStyle {
  late StyleType type;
  Color? backgroundColor;
  AnimationEffectTemplate? effectTemplate;


  BackgroundStyle({
    required this.type,
    this.backgroundColor,
    this.effectTemplate,

  }) ;


  BackgroundStyle.fromJson(Map<String, dynamic> map) {
    type = StyleType.values[map['type']];

    final colorValue = map['backgroundColor'];
    if (colorValue != null) {
      backgroundColor = Color(colorValue);
    }
    if (map['effectTemplate'] != null) {
      effectTemplate = AnimationEffectTemplate.fromJson(map['effectTemplate']);
    }
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'type': type.index,
      'backgroundColor': backgroundColor?.value,
      'effectTemplate': effectTemplate?.toJson(),
    };
  }

  BackgroundStyle copyWith(
      {StyleType? type,
        Color? backgroundColor,
        AnimationEffectTemplate? effectTemplate}) {
    return BackgroundStyle(
        type: type ?? this.type,
        backgroundColor: backgroundColor ?? this.backgroundColor,
        effectTemplate: effectTemplate ?? this.effectTemplate);
  }
}